/**
 *	Módulo de Simuladores
 *	@author Gary Rios
 */

window.sim = (function() {

/**
 *	Calculate the range where a value belongs to
 *	@param {Number} value The value to test.
 *	@param {Number[]} ranges An array of the range delimiter values.
 *	@param {Object[]} convert Optional. An object in the corresponding index of an array.
 *	@return The range where the value belongs 
 */
function calculateRange(value, ranges, convert) {
	var i, length;
	for(i = 0, length = ranges.length; i < length - 1; i++) {
		if(value >= ranges[i] && value <= ranges[i + 1]) return convert ? convert[i] : i;
	}
}
/**
 *	Parse an object's property values to their types
 *	@param {Object} input The object to be modified
 */
function parseData(input) {
	$.each(input, function(key, value) {
		if($.isNumeric(parseFloat(value))) input[key] = parseFloat(value);
		if(value === "true") input[key] = true;
		if(value === "false") input[key] = false;
	});
}
/**
 *	Personas - Libre disponibilidad
 *	@param {Object} input The form input values.
 *	@param {Object} data The data object.
 *	@return undefined
 */
function personasLibre(input, data) {
	var table, segment, tea, tem, q1, fee, totalFee;
	parseData(input);
	table = input.sbk ? "tabla2a" : "tabla2b";
	segment = calculateRange(input.salary, data[input.curSalary]["tabla1"]);
	tea = calculateRange(input.amount, data[input.curAmount]["tabla2"], data[input.curAmount][table])[segment] / 100;
	tem = Math.pow(1 + tea, 1 / 12) - 1;
	q1 = Math.pow(1 + tem, input.months);
	fee = input.amount * tem * q1 / (q1 - 1);
	totalFee = fee + data[input.curAmount]["delivery"][input.delivery];
	
	return {
		tea: (tea * 100).toFixed(2),
		totalFee: totalFee
	}
}

return {
	personasLibre: personasLibre
}

})();